package bawei.com.adapter;


import android.content.Context;

import android.support.v7.widget.RecyclerView;


import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import bawei.com.bilibili.R;
import bawei.com.model.banner.GlideImageLoader;
import bawei.com.model.bean.To_them_bean1;
import bawei.com.model.bean.To_them_bean2;


/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/13 11:39
 * 邮箱:18310832074@163.com
 */

public
class
To_them_Adapter extends RecyclerView.Adapter {

    private Context context;
    private To_them_bean1 themBean1s;
    private To_them_bean2 themBean2s;
    List<String> stringList = new ArrayList<>();
    private final static int Banner = 0;
    private final static int LianZai = 1;
    private final static int LianZaiNeiRong = 2;
    private final static int TuiJian = 3;
    private final static int TuiJianNeiRong = 4;
    int a = -1;
    int b = -1;
    int c = 0;

    public To_them_Adapter(Context context, To_them_bean1 themBean1s, To_them_bean2 themBean2s) {
        this.context = context;
        this.themBean1s = themBean1s;
        this.themBean2s = themBean2s;
    }

    public int getSpanSize(int pos) {

        int itemViewType = getItemViewType(pos);
        switch (itemViewType) {

            case Banner:
                return 12;
            case LianZai:
                return 12;
            case LianZaiNeiRong:
                return 4;
            case TuiJian:
                return 12;
            case TuiJianNeiRong:
                return 12;

        }
        return 0;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {

            case Banner:
                view = LayoutInflater.from(context).inflate(R.layout.item_live_banner, parent, false);
                return new Drama_series_banner(view);
            case LianZai:
                view = LayoutInflater.from(context).inflate(R.layout.drama_series_item1, parent, false);
                return new Drama_series_item1(view);
            case LianZaiNeiRong:
                view = LayoutInflater.from(context).inflate(R.layout.drama_series_item2, parent, false);
                return new Drama_series_item2(view);
            case TuiJian:
                view = LayoutInflater.from(context).inflate(R.layout.drama_series_item3, parent, false);
                return new Drama_series_item3(view);
            case TuiJianNeiRong:
                view = LayoutInflater.from(context).inflate(R.layout.drama_series_item4, parent, false);
                return new Drama_series_item4(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof Drama_series_banner) {

            List<To_them_bean1.ResultBean.AdBean.HeadBean> head = themBean1s.getResult().getAd().getHead();
            stringList.clear();
            for (int i = 0; i < head.size(); i++) {
                stringList.add(head.get(i).getImg());
            }

            GlideImageLoader glideImageLoader = new GlideImageLoader();
            ((Drama_series_banner) holder).banner.setImageLoader(glideImageLoader)
                    .setImages(stringList)
                    .setBannerStyle(BannerConfig.RIGHT)
                    .setBannerStyle(BannerConfig.NUM_INDICATOR)
                    .start();

        } else if (holder instanceof Drama_series_item1) {

            if (position == 1) {
                ((Drama_series_item1) holder).imageView1.setImageResource(R.drawable.ic_lianzai);
                ((Drama_series_item1) holder).textView1.setText("新番连载");
                ((Drama_series_item1) holder).textView2.setText("所有连载");
            } else if (position == 8) {
                ((Drama_series_item1) holder).imageView1.setImageResource(R.drawable.bangumi_home_ic_season_4);
                ((Drama_series_item1) holder).textView1.setText("10月新番");
                ((Drama_series_item1) holder).textView2.setText("分季列表");
            }
        } else if (holder instanceof Drama_series_item2) {

            if (position > 1 && position < 8) {
                List<To_them_bean1.ResultBean.SerializingBean> serializing = themBean1s.getResult().getSerializing();
                b++;
                if (b == 6) {

                    b = 0;
                }
                Glide.with(context).load(serializing.get(b).getCover())
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(((Drama_series_item2) holder).imageView);
                ((Drama_series_item2) holder).follow_text.setText(serializing.get(b).getWatching_count() + "人在看");
                ((Drama_series_item2) holder).drama_series_item2_text1.setText(serializing.get(b).getTitle());
                ((Drama_series_item2) holder).drama_series_item2_text2.setVisibility(View.VISIBLE);

            } else if (position > 8 && position < 12) {

                List<To_them_bean1.ResultBean.PreviousBean.ListBean> list = themBean1s.getResult().getPrevious().getList();
                a++;
                if (a == 3) {
                    a = 0;
                }
                Glide.with(context).load(list.get(a).getCover())
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(((Drama_series_item2) holder).imageView);
                ((Drama_series_item2) holder).follow_text.setText(list.get(a).getWatching_count() + "人追番");
                ((Drama_series_item2) holder).drama_series_item2_text1.setText(list.get(a).getTitle());
                ((Drama_series_item2) holder).drama_series_item2_text2.setVisibility(View.GONE);

            }

        } else if (holder instanceof Drama_series_item3) {

            ((Drama_series_item3) holder).textView.setText("番剧推荐");
            ((Drama_series_item3) holder).imageView.setImageResource(R.drawable.bangumi_home_ic_season_4);

        } else if (holder instanceof Drama_series_item4) {

            List<To_them_bean2.ResultBean> result = themBean2s.getResult();
            Glide.with(context).load(result.get(c).getCover())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(((Drama_series_item4) holder).imageView);
            ((Drama_series_item4) holder).textView1.setText(result.get(c).getTitle());
            ((Drama_series_item4) holder).textView2.setText(result.get(c).getDesc());
            c++;
            if(c==10){

            c=0;
            }
        }
        if (position == getItemCount() - 1) {
            a = 0;
            b = 0;
            c = 0;
        }

    }

    @Override
    public int getItemCount() {

        To_them_bean1.ResultBean result = themBean1s.getResult();
        int size = result.getAd().getHead().size();
        List<To_them_bean1.ResultBean.PreviousBean.ListBean> list = themBean1s.getResult().getPrevious().getList();
        List<To_them_bean1.ResultBean.SerializingBean> serializing = themBean1s.getResult().getSerializing();
        return 1 + 3 + size + list.size() + serializing.size() + themBean2s.getResult().size();
    }


    @Override
    public int getItemViewType(int position) {

        if (position == 0) {

            return Banner;

        } else if (position == 1 || position == 8) {

            return LianZai;
        } else if (position > 1 && position < 8) {

            return LianZaiNeiRong;
        } else if (position > 8 && position < 12) {

            return LianZaiNeiRong;
        } else if (position == 12) {

            return TuiJian;

        } else if (position > 12) {
            return TuiJianNeiRong;
        }
        return 0;
    }

    //轮播图
    class Drama_series_banner extends RecyclerView.ViewHolder {

        Banner banner;

        public Drama_series_banner(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);
        }
    }

    //头部连载标题
    class Drama_series_item1 extends RecyclerView.ViewHolder {

        ImageView imageView1;
        TextView textView1, textView2;

        public Drama_series_item1(View itemView) {
            super(itemView);
            imageView1 = (ImageView) itemView.findViewById(R.id.drama_series_item1_image);
            textView1 = (TextView) itemView.findViewById(R.id.drama_series_item1_text1);
            textView2 = (TextView) itemView.findViewById(R.id.drama_series_item1_text2);
        }
    }

    //连载下面的数据
    class Drama_series_item2 extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView follow_text, drama_series_item2_text1, drama_series_item2_text2;

        public Drama_series_item2(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.drama_series_item2_image);
            follow_text = (TextView) itemView.findViewById(R.id.follow_text);
            drama_series_item2_text1 = (TextView) itemView.findViewById(R.id.drama_series_item2_text1);
            drama_series_item2_text2 = (TextView) itemView.findViewById(R.id.drama_series_item2_text2);
        }
    }

    //番剧推荐标题
    class Drama_series_item3 extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

        public Drama_series_item3(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.drama_series_item3_image);
            textView = (TextView) itemView.findViewById(R.id.drama_series_item3_text1);
        }
    }

    //番剧推荐
    class Drama_series_item4 extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView1, textView2;

        public Drama_series_item4(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.drama_series_item4_image);
            textView1 = (TextView) itemView.findViewById(R.id.drama_series_item4_text1);
            textView2 = (TextView) itemView.findViewById(R.id.drama_series_item4_text2);
        }
    }


}
